package com.yozinga.util;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class SinglePlaceInfo {

	// flag for Internet connection status
	Boolean isInternetPresent = false;


	// Alert Dialog Manager
	AlertDialogManager alert = new AlertDialogManager();

	// Google Places
	GooglePlaces googlePlaces;

	// Place Details
	String placeDetails;

	// Progress dialog
	ProgressDialog pDialog;

	// KEY Strings
	public static String KEY_REFERENCE = "reference"; // id of the place

	Context mContext;
	String mAddress;
	ListListener mListListener;
	public SinglePlaceInfo(Context context,String reference,ListListener listener,String address) {
		mContext = context;
		mListListener = listener;
		mAddress = address;
		new LoadSinglePlaceDetails().execute(reference);

	}


	public interface ListListener{
		public void locationValue(String latitude,String longitude,String address);
	}

	/**
	 * Background Async Task to Load Google places
	 * */
	class LoadSinglePlaceDetails extends AsyncTask<String, String, String> {

		/**
		 * Before starting background thread Show Progress Dialog
		 * */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(mContext);
			pDialog.setMessage("Loading profile ...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(false);
			pDialog.show();
		}

		/**
		 * getting Profile JSON
		 * */
		protected String doInBackground(String... args) {
			String reference = args[0];

			// creating Places class object
			googlePlaces = new GooglePlaces();
			Log.i("Today","doInBackground reference is"+reference);
			// Check if used is connected to Internet
			try {
				placeDetails = googlePlaces.getPlaceDetails(reference);

				Log.i("Today","Place Details are : "+placeDetails);

			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}

		/**
		 * After completing background task Dismiss the progress dialog
		 * **/
		protected void onPostExecute(String file_url) {
			// dismiss the dialog after getting all products
			pDialog.dismiss();
			
			/**
			 * Updating parsed Places into LISTVIEW
			 * */
			if(placeDetails != null){
				try{
					JSONObject jsonObject = new JSONObject(placeDetails);
					JSONObject resultjsonObject = jsonObject.getJSONObject("result");

					
					String address = resultjsonObject.getString("formatted_address");
					Log.i("Today","placeDetails.status"+address);

					JSONObject geometryjsonObject = resultjsonObject.getJSONObject("geometry");
					JSONObject locationjsonObject = geometryjsonObject.getJSONObject("location");

					String latitude = locationjsonObject.getString("lat");
					String longitude = locationjsonObject.getString("lng");

					Log.i("Today","latitude"+latitude);
					Log.i("Today","latitude"+longitude);
					address = address == null ? "Not present" : address;

					latitude = latitude == null ? "Not present" : latitude;
					longitude = longitude == null ? "Not present" : longitude;
					mListListener.locationValue(latitude, longitude, mAddress);
				}
				catch(JSONException jsonException){

				}
			}else{
				/*alert.showAlertDialog(mContext, "Places Error",
						"Sorry error occured.",
						false);*/
			}
		}
	}
}